package com.example.s.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.s.Activity.BaseActivity;
import com.example.s.R;
import com.example.s.network.MyNetwork;

public abstract class BaseFragment extends Fragment {
    public OnRefreshListener<String> listener;
    public BaseActivity A;
    public MyNetwork N;
    public String TAG;

    public void loadImg(ImageView imageView, String src,@Nullable Boolean isLarge) {
        if (imageView == null || "".equals(src)) {
            throw new NullPointerException("加载图片或src为空");
        }
        Glide.with(A).load(MyNetwork.BASE_URL + src.substring(2)+(!isLarge||isLarge==null?"":"?isLarge=true")).into(imageView);
    }public void loadImg(ImageView imageView, String src) {
        if (imageView == null || "".equals(src)) {
            throw new NullPointerException("加载图片或src为空");
        }
        Glide.with(A).load(src).into(imageView);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.A = (BaseActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.from(container.getContext()).inflate(getLayoutId(), container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        TAG = getClass().getSimpleName();
        N = MyNetwork.getInstance(A);
        initView(getView());
    }

    public void setOnRefreshListener(OnRefreshListener listener) {
        this.listener = listener;
    }

    public interface OnRefreshListener<T> {
        T onRefresh(String tag);

        T onLoad(String tag);

    }

    public abstract int getLayoutId();

    public abstract void initView(View view);

    public View findViewById(int id) {
        return getView().findViewById(id);
    }
}
